/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-06-05     LENOVO       the first version
 */

#include <rtdbg.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <rtdef.h>
#include <stdio.h>
#include <string.h>

#include <drivers/pin.h>
#include <drivers/serial.h>
#include <SU_03T_USART.h>

uint8_t received_data;

/** 串口相关定义  */
#define SAMPLE_UART_NAME       "uart2"    /* 串口设备名称 */
static rt_device_t uart2_dev;                /* 串口设备句柄 */
struct serial_configure uart2_configs = RT_SERIAL_CONFIG_DEFAULT;
struct rt_semaphore uart2_sem;
rt_thread_t uart2_th;
rt_size_t rx_len;
void uart2_thread_enter(void *parameter);
rt_err_t rx_callback(rt_device_t dev , rt_size_t size);


/**
  * @brief  初始化串口用于接收 SU-03T 数据
  * @param  无
  * @retval 无
  */
void SU_03T_USART_Config ( void )
{
    rt_err_t uart_err = 0;

    /* 查找串口设备 */
    uart2_dev = rt_device_find(SAMPLE_UART_NAME);
    if (uart2_dev == RT_NULL) {

        rt_kprintf("No find uart2 \n");
        return;
    }

    /*  配置设备  */
    rt_device_control(uart2_dev, RT_DEVICE_CTRL_CONFIG, (void*)&uart2_configs);


    /* 打开串口设备  */
    uart_err = rt_device_open(uart2_dev,  RT_DEVICE_FLAG_DMA_RX);

    if (uart_err < 0) {

        rt_kprintf("uart2 open failed \n");
        return;
    }

    /* 设置中断回调  */
    rt_device_set_rx_indicate(uart2_dev, rx_callback);

    /* 线程相关
     * 信号量的资源值设置为0，因为我们一开始是没有数据的。只有当对方发送数据之后，调用接收回调函数，在回调函数中发送信号量，这样信号量的value值就会加1，从而唤醒接收线程。
     * 第四个参数模式设置为先进先出
     */
    rt_sem_init(&uart2_sem, "uart2_sem", 0 , RT_IPC_FLAG_FIFO);
    /*创建线程*/
    uart2_th = rt_thread_create("uart2", uart2_thread_enter, NULL, 1024, 10, 5);
    /* 启动 */
    rt_thread_startup(uart2_th);

    rt_kprintf("uart2 init success! \n");
    return;
}

/***********************************************************************************************************/
/**
  * @brief  USART接收回调函数
  * @param
  * @retval
  */
rt_err_t rx_callback(rt_device_t dev , rt_size_t size)
{
    rx_len = size;
    rt_sem_release(&uart2_sem);
    return RT_EOK;
}

/**
  * @brief  USART接收线程函数
  * @param
  * @retval
  */
void uart2_thread_enter(void *parameter)
{
    rt_size_t ret_len;
    char buffer[128] = {0};
    while(1){
        rt_sem_take(&uart2_sem, RT_WAITING_FOREVER); // 等待串口中断信号
        ret_len = rt_device_read(uart2_dev, 0, buffer, rx_len);

        buffer[ret_len] =  '\0';

        if ((unsigned char)buffer[0] == 0x10) {
            rt_kprintf("Received 0x10\n");//前进
            received_data = 0x10; // 将数据传递给 PetAction.c
        }else if ((unsigned char)buffer[0] == 0x20) {
            rt_kprintf("Received 0x20\n");//后退
            received_data = 0x20; // 将数据传递给 PetAction.c
        }else if ((unsigned char)buffer[0] == 0x30) {
            rt_kprintf("Received 0x30\n");//左转
            received_data = 0x30; // 将数据传递给 PetAction.c
        }else if ((unsigned char)buffer[0] == 0x40) {
            rt_kprintf("Received 0x40\n");//右转
            received_data = 0x40; // 将数据传递给 PetAction.c
        }else if ((unsigned char)buffer[0] == 0x50) {
            rt_kprintf("Received 0x50\n");//趴下
            received_data = 0x50; // 将数据传递给 PetAction.c
        }else if ((unsigned char)buffer[0] == 0x60) {
            rt_kprintf("Received 0x60\n");//起立
            received_data = 0x60; // 将数据传递给 PetAction.c
        }else if ((unsigned char)buffer[0] == 0x70) {
            rt_kprintf("Received 0x70\n");//握手
            received_data = 0x70; // 将数据传递给 PetAction.c
        }else if ((unsigned char)buffer[0] == 0x80) {
            rt_kprintf("Received 0x80\n");//摇尾巴
            received_data = 0x80; // 将数据传递给 PetAction.c
        }
        rt_thread_mdelay(100);
    }
}

//MSH_CMD_EXPORT(SU_03T_USART_Config, Initialize the USART3 for receiving SU-03T data);
